/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.dao;

import br.com.deployment.entity.selo.Ato;
import br.com.deployment.interfacedao._AtoDao;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author elyjr
 */
@Stateless
@LocalBean
public class AtoDaoImpl implements _AtoDao {

    @PersistenceContext
    EntityManager em;
    
    @Override
    public void create(Ato ato) {
        em.persist(ato);
    }

    @Override
    public void edit(Ato ato) {
      em.merge(ato);
    }

    @Override
    public void remove(Ato ato) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Ato find(int id) {
        System.err.println("------------>"+id);
        return em.find(Ato.class, id);
    }

    @Override
    public Ato findDescricao(String descricao) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Ato> findAll() {
        List<Ato> lista = new ArrayList<Ato>();
        String sql = "select a from Ato a ";        
        Query query = em.createQuery(sql);        
        lista = query.getResultList();
        return lista;
    }

    @Override
    public List<Ato> findAto(Ato ato) {
        List<Ato> list = new ArrayList<Ato>();
        if(ato != null){
             String sql = "Select a from Ato a ";
             
             if(ato.getDescricao()!= null && ato.getDescricao() != ""){
                 sql += "  where lower(a.descricao) like lower(?1) and"; 
             }
              if(ato.getCodigo()!= null && ato.getCodigo() != ""){
                 sql += "  where lower(a.codigo) like lower(?2) and"; 
             }
              
             sql += " 1=1 ";
             Query query = em.createQuery(sql);
             if(ato.getDescricao()!= null && ato.getDescricao() != ""){
                 query.setParameter("1", "%" + ato.getDescricao() + "%");
             }
             
             if(ato.getCodigo()!= null && ato.getCodigo() != ""){
                 query.setParameter("2", "%" + ato.getCodigo() + "%");
             }
        
        
              list = query.getResultList();
              System.out.println(sql);
        }
        
        return list;
    
    }

    @Override
    public List<Ato> findAto(String desc){
        
        List<Ato> lst = new ArrayList<Ato>();
        String sql = "Select a from Ato where a.descricao like :desc";
        
        Query query = em.createQuery(sql);
        query.setParameter(":desc", "%"+desc+"%");
        
        lst = query.getResultList();
        
        return lst;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public List<Ato> findStatus() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
